/*
 * Created by: vadikv
 * Created: Tuesday, March 04, 2008
 */

using System.Collections.Generic;
using System.Text;
using fit;
using OneDayModel.Interfaces;
using OneDayModel.PresentationLogic;
using OneDayModel.Tests.Fits.UIUsers;

namespace OneDayModel.Tests.Fits.Fixtures.Scenarios
{
    public class NewUserProfileFixture : Fixture
    {
        private NewUserProfileUser newUserProfileUser;
        private string exceptionThrown = string.Empty;

        public void NewUser()
        {
            UsersFactory userFactory =
                (UsersFactory) TestUtils.ContextManager.TestContext[typeof (UsersFactory).ToString()];
            newUserProfileUser = userFactory.GetNewUserProfileUser();

        }

        public string MissedAttributesInfoShown()
        {
            return newUserProfileUser.MissedAttributesInfoShown;
        }

        public string ReturnToQuestionerOptionShown()
        {
            return newUserProfileUser.BackToQuestionerShown;
        }

        public string NewEMailOptionShown()
        {
            return newUserProfileUser.NewEMailOptionShown;
        }

        public string RetrieveCredantialsOptionShown()
        {
            return newUserProfileUser.RetrieveCredantialsShown;
        }

        public string ExceptionThrown()
        {
            return exceptionThrown;
        }

        public string AvailableRoles()
        {
            return newUserProfileUser.AvailableRoles;
        }
        
        public void SelectedRoles(string roles)
        {
            string[] selectedRoles = roles.Split(';');
            newUserProfileUser.SetSelectedRoles(selectedRoles);
        }
        
        public string ShownRequiredAttributes()
        {
            return newUserProfileUser.ShownRequiredAttributes;
        }

        public string ShownOptionalAttributes()
        {
            return newUserProfileUser.ShownOptionalAttributes;
        }

        public void FillData(string data)
        {
            string[] dataArray = data.Split(';');
            KeyValuePair<string, string>[] attributes = new KeyValuePair<string, string>[dataArray.Length];
            int i = 0;
            foreach (string attrData in dataArray)
            {
                string[] attr = attrData.Split('=');
                attributes[i++] = new KeyValuePair<string, string>(attr[0], attr[1]);
            }
            newUserProfileUser.FillAttributes(attributes);
        }
        
        public void SaveProfile()
        {
            try
            {
                newUserProfileUser.SaveProfile();
                exceptionThrown = string.Empty;
            }
            catch (AliasNotUniqueException e)
            {
                exceptionThrown = e.Message;
            }
            catch (AccountAlreadyExistException e)
            {
                exceptionThrown = e.Message + (e.AccountStatus == AccountStatus.existAndBanned?" and banned":"");
            }
            catch(MissingRequiredValuesException e)
            {
                StringBuilder exText = new StringBuilder("Missing values for following attributes: ");
                exText.Append(e.MissedAttributes[0]);
                for(int i=1; i<e.MissedAttributes.Length; i++)
                {
                    exText.Append(", ");
                    exText.Append(e.MissedAttributes[i]);
                }

                exceptionThrown = exText.ToString();
            }
        }

        public void ReturnToQuestioner()
        {
            newUserProfileUser.ReturnToQuestioner();
        }
    }
}